package org.xlp.mvc.definition;

import org.xlp.bean.base.AbstractBeanDefinition;
import org.xlp.bean.base.IBeanFields;
import org.xlp.bean.base.IBeanWrapper;
import org.xlp.mvc.annotation.Controller;

/**
 * 注解{@link Controller}标记类的bean定义
 *
 * Create by xlp on 2023/8/25
 */
public class ControllerAnnotationBeanDefinition extends AbstractBeanDefinition {
    private Controller controller;

    public ControllerAnnotationBeanDefinition(IBeanFields beanFields) {
        super(beanFields);
        Class<?> beanClass = beanFields.getBeanClass();
        setControllerAnnotation(beanClass);
    }

    public ControllerAnnotationBeanDefinition(Class<?> beanClass) {
        super(beanClass);
        setControllerAnnotation(beanClass);
    }

    public ControllerAnnotationBeanDefinition(String beanClassName) {
        super(beanClassName);
        setControllerAnnotation(beanClass);
    }

    @Override
    public boolean isProxy() {
        return IBeanWrapper.class.isAssignableFrom(getBeanClass());
    }

    @Override
    public boolean isSingleton() {
        return false;
    }

    @Override
    public boolean isLazy() {
        return true;
    }

    @Override
    public String getBeanId() {
        return beanClass.getName();
    }

    @Override
    public String getDescription() {
        return controller.description();
    }

    /**
     * 设置Controller注解
     * @param beanClass bean 类型
     * @throws IllegalArgumentException 假如给定的类没有{@link Controller}注解标记，则抛出该异常
     */
    private void setControllerAnnotation(Class<?> beanClass) {
        this.controller = beanClass.getDeclaredAnnotation(Controller.class);
        if (controller == null){
            throw new IllegalArgumentException("给的的类[" + beanClass.getName() + "]没有[" + Controller.class.getName() + "]注解标记");
        }
    }
}
